<?php
namespace app\admin\controller;

use app\http\Controller;
use Zewail\Api\Facades\JWT;
use app\model\admin\Employee;
use think\Request;

class Guest extends Controller {

    public function login()
    {
        return '未登入';

    }


    //后台admin 登入
    public function putLogin(Request $req)
    {

//        $credentials = ['account'=>'wubuze@qq.com', 'pwd' => '1234567'];
        $account = $req->param('account');
        $pwd = $req->param('pwd');
        if (!$account || !$pwd) {
            return $this->resFail('账号和密码不能为空 !');
//            return 'error';
        }

        $credentials = ['account'=> $account, 'pwd' => $pwd];

        $token = JWT::attempt($credentials);

        if ($token) {
            return $this->resSuccess('登入成功', [
                'token' => $token
            ]);
        } else {
            return $this->resFail('用户名或密码错误 !');
        }

    }


    // 注册admin 用户
    public function register(){

        $account = input('account');
        $pwd = input('pwd');
        if (!$account || !$pwd) {
            return $this->resFail('账号和密码不能为空 !');
//            return 'error';
        }
        $acc = Employee::where('account', $account)->find();

        if ($acc) {
            return $this->resFail('账号已经存在!');
//            return 'account exist';
        }

        $data = [
            'account' => $account,
            'pwd' => md5(sha1($pwd)),
            'name' => input('name')
        ];

        $res = Employee::insert($data);;


        return $res ?  $this->resSuccess('操作成功') : $this->resFail('操作失败 !') ;

    }
}